package org.dtrd.modules.record.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.record.entity.bean.NutrientDailyInfo;
import org.dtrd.modules.record.entity.bean.NutrientListInfo;
import org.dtrd.modules.record.entity.bean.NutrientRecordInfo;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.INutrientRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @author qr
 * @date 2021/12/6 10:52
 */
@Api(tags = "营养素打卡")
@Slf4j
@RestController
@RequestMapping("/101/12/")
public class NutrientController extends BaseController {

    @Autowired
    private INutrientRecordService nutrientRecordService;

    @ApiOperation("保存或修改营养素记录")
    @PostMapping("1011201")
    @AutoLog(value="保存或修改营养素记录", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateNutrientRecord(@RequestBody NutrientRecordInfo nutrientRecordInfo) {
        if (isIntParamInvalid(nutrientRecordInfo.getPatientId())
                || nutrientRecordInfo.getNutrientRecordType() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = nutrientRecordService.saveOrUpdateNutrientRecord(nutrientRecordInfo);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("获取营养素列表")
    @PostMapping("1011202")
    @AutoLog(value="获取营养素列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getNutrientList(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            Integer page = getIntParam(request.getPage(), 1);
            Integer pageSize = getIntParam(request.getPageSize(), 20);
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            Integer count = nutrientRecordService.getNutrientRecordDataCount(patientId, startDate, endDate);
            JSONObject result = new JSONObject();
            if (count != null && count > 0) {
                List<NutrientDailyInfo> list = nutrientRecordService.getNutrientRecordList(page, pageSize, patientId, startDate, endDate);
                result.put("recordList", list);
                result.put("totalPage", count <= pageSize || pageSize <= 0 ? 1 : count / pageSize + 1);
                result.put("count", count);
            } else {
                result.put("recordList", Collections.EMPTY_LIST);
                result.put("totalPage", 1);
                result.put("count", 0);
            }
            return Result.OK(result);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }
}
